package com.cscec.omega.controller;

import com.cscec.omega.service.OSSService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/api/oss")
public class OSSController {

    @Autowired
    private OSSService ossService;

    @GetMapping("/bucket/upLoad")
    public ResponseEntity upLoad(MultipartFile file){
        String objectKey = UUID.randomUUID().toString();
        try {
            ossService.upload(objectKey, file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResponseEntity.ok(objectKey);
    }

    @GetMapping("/bucket/downLoad")
    public void downLoad(String objectKey, HttpServletResponse response){
        try {
            ossService.download(objectKey, response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/bucket/delete")
    public void deleteFile(String objectKey){
        ossService.delete(objectKey);
    }
}
